﻿using DeviceModule.Models;
using DeviceModule.Services;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceModule.ViewModels
{
    public class DeviceListViewModel : BindableBase
    {
        // 设备状态集合，绑定到 DataGrid
        public ObservableCollection<DeviceStatus> Devices { get; } = new();

        // 模拟心跳按钮绑定的命令
        public DelegateCommand SimulateCommand { get; }

        private readonly IDeviceParserRegistry _registry;

        public DeviceListViewModel(IDeviceParserRegistry registry)
        {
            _registry = registry;

            // 注册一个模拟解析器（PLC）
            _registry.Register("PLC", new PlcMockParser());

            SimulateCommand = new DelegateCommand(SimulateHeartbeat);
        }

        private void SimulateHeartbeat()
        {
            var parser = _registry.GetParser("PLC");
            var status = parser?.Parse(new byte[] { 0x01, 0x02 }); // 模拟数据
            if (status != null)
                Devices.Add(status);
        }
    }
}